package com.maduar.batch.demo.user;

import com.maduar.batch.entity.Myuser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

/**
 * Description:
 *
 * @Author maduar maduar@163.com
 * @Date 2021-07-12 23:04
 */
@Slf4j
@Configuration
@EnableBatchProcessing
public class UserMysqlJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    private String jobName = "UserMysqlJob2";
    private String step1 = "UserMysqlJob-step1";

    @Bean
    public Job importUserJob() {
        return jobBuilderFactory.get(jobName)
                .flow(userMysqlJobStep1())
                .end()
                .build();
    }

    @Bean
    public Step userMysqlJobStep1() {
        return stepBuilderFactory.get(step1)
                .<Myuser, Myuser> chunk(3)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }

    @Bean
    public FlatFileItemReader<Myuser> reader() {
        FlatFileItemReader<Myuser> reader = new FlatFileItemReader<>();

        reader.setResource(new ClassPathResource("data/users.csv"));
        reader.setLineMapper(new DefaultLineMapper<Myuser>(){{
            setLineTokenizer(new DelimitedLineTokenizer(){{
                setNames(new String[]{"name"});
                setDelimiter(",");
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Myuser>(){{
                setTargetType(Myuser.class);
            }});
        }});


        return reader;
    }

    @Bean
    public JdbcBatchItemWriter<Myuser> writer() {
        JdbcBatchItemWriter<Myuser> writer = new JdbcBatchItemWriter<>();
        writer.setItemSqlParameterSourceProvider(
                new BeanPropertyItemSqlParameterSourceProvider<Myuser>()
        );
        writer.setSql("INSERT INTO myuser(name) VALUES (:name)");
        writer.setDataSource(dataSource);
        return writer;
    }

    @Bean
    public UserItemProcessor processor() {
        return new UserItemProcessor();
    }

    public class UserItemProcessor implements ItemProcessor<Myuser, Myuser> {
        @Override
        public Myuser process(Myuser myuser) {
            return myuser;
        }
    }
}
